<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 检测昵称是否可用
 * @param 
 *         $nick = 作用
 * 
 * 
 */
include('../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_user.php');
class aj_personSetting extends ml_controller 
{
    private $pA;
    private $pB;
    private $pE;

    private $arrAlert = array(
        'nick_format' => '昵称格式不正确',
        'nick_len' => '昵称长度不正确',
        'nick_exists' => '昵称已经被占用',
        'nick_danger' => '请检查，昵称中含有敏感字符',
        'occupation_len' => 'occupation_len',
        'weibo_address' => 'weibo_address',
        'declaration_len'=>'declaration_len',
    );

    function initParam()
    {
        $this->pA['nickname'] = $this->input('nickname','P');

        $this->pB['astro'] = (int)$this->input('astro','P');
        $this->pB['access_occupation'] = (int)$this->input('access_occupation','P');
        $this->pB['occupation'] = $this->input('occupation','P');
        $this->pB['weibo_address'] = $this->input('weibo_address','P');
        $this->pB['declaration'] = Tool_keyword::filterSensitiveWord($this->input('declaration','P'));



        $this->pE['gender'] = (int)$this->input('gender','P');
        $this->pE['birthday'] = $this->input('birthday','P');
        $this->pE['blood_type'] = $this->input('blood_type','P');
        $this->pE['province_id'] = $this->input('province_id','P');
        $this->pE['city_id'] = $this->input('city_id','P');
        $this->pE['interest_tags'] = $this->input('interest_tags','P');
    }
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam()
    {
        $this->check_referer();
        if(!$this->check_permission(ML_PERMISSION_LOGIN_ONLY)) {
            return $this->api_output(ML_RCODE_NOLOGIN);
        }
        else if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            return $this->api_output(ML_RCODE_NOACTIVE);
        }

        /*用户昵称检查*/
        if(!empty($this->pA['nickname'])){
            $nickname = $this->__visitor['nickname'];
            if($nickname == $this->pA['nickname']){
                $this->pA['nickname'] = false;
            }else{
                if(!Tool_keyword::check_article_kill_keyword($this->pA['nickname'],'utf8')){
                    return $this->api_output(ML_RCODE_PARAM,'',$this->arrAlert['nick_format']);
                }
                
                if(!preg_match(ML_DATARULE_USER_NICK_PREG , $this->pA['nickname']))
                    $this->api_output(ML_RCODE_PARAM ,'', $this->arrAlert['nick_format']);
                    
                $len = Tool_string::str_width($this->pA['nickname']);
                if($len < ML_DATARULE_USER_NICK_MINLEN || $len > ML_DATARULE_USER_NICK_MAXLEN)
                    $this->api_output(ML_RCODE_PARAM , '' , $this->arrAlert['nick_len']);
            }
        }

        //$this->pE['gender'] = ML_DATARULE_USER_GENDER_GIRL;男女都可以了

        /*
        * 检查生日和星座
        */
        if(!empty($this->pE['birthday'])){
            if(!strtotime($this->pE['birthday'])){
                $this->api_output(ML_RCODE_PARAM);
            }
            if(empty($this->pB['astro'])) $this->api_output(ML_RCODE_PARAM);

            $datetime = strtotime($this->pE['birthday']);
            $datetimeA = explode('-', $this->pE['birthday']);
            $year = (int)$datetimeA[0];
            $astros = ml_factory::load_standard_conf('astro');

            $curAstors = $astros[$this->pB['astro']];
            if(empty($curAstors)) $this->api_output(ML_RCODE_PARAM);

            if($this->pB['astro'] == 1){
                $starttime = strtotime(($year-1).'-'.$curAstors[0]);
            }else{
                $starttime = strtotime($year.'-'.$curAstors[0]);
            }
            $endtime = strtotime($year.'-'.$curAstors[1]);

            if(!($datetime>=$starttime && $datetime<$endtime)) $this->api_output(ML_RCODE_PARAM);
        }else{
            if(!empty($this->pB['astro'])) $this->api_output(ML_RCODE_PARAM);
        }

        /**
        * 检查血型
        */
        if(!empty($this->pE['blood_type'])){
            $bloods = ml_factory::load_standard_conf('bloods');
            if(empty($bloods[$this->pE['blood_type']])) $this->api_output(ML_RCODE_PARAM);
        }

        /*省市检查*/
        if(!empty($this->pE['province_id'])){
            $province = ml_factory::load_standard_conf('province');
            $city = ml_factory::load_standard_conf('citys');
            if(empty($province[$this->pE['province_id']])){
                $this->api_output(ML_RCODE_PARAM);
            }
            if(!empty($this->pE['city_id'])){
                $prov = substr($this->pE['province_id'],0,2);
                $ci = substr($this->pE['city_id'],0,2);
                if($prov != $ci){
                    $this->api_output(ML_RCODE_PARAM);
                }
                if(empty($city[$this->pE['city_id']])){
                    $this->api_output(ML_RCODE_PARAM);
                }
            }
        }else{
            if(!empty($this->pE['city_id'])){
                $this->api_output(ML_RCODE_PARAM);
            }
        }

        if(Tool_string::str_width($this->pB['occupation'])>ML_DATARULE_USER_OCCUPATION_MAXLEN)
            $this->api_output(ML_RCODE_PARAM , '', $this->arrAlert['occupation_len']);
          
        if($this->pB['weibo_address'] && $this->pB['weibo_address']!='http://' && !preg_match(ML_DATARULE_USER_WEIBO_ADDRESS , $this->pB['weibo_address']))
            $this->api_output(ML_RCODE_PARAM ,'', $this->arrAlert['weibo_address']);

        if($this->pB['declaration'] && Tool_string::str_width($this->pB['declaration'])>ML_DATARULE_USER_DECLARATION_MAXLEN)
            $this->api_output(ML_RCODE_PARAM , '', $this->arrAlert['declaration_len']);


    }
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main()
    {

        $oBrief = new ml_model_dbUserBriefInfo();
        $oExt = new ml_model_dbUserExtInfo();
        
        $uid = $this->__visitor['uid'];

        if(!empty($this->pA['nickname'])){    
            $oAccount = new ml_model_dbUserAccount();
            if(false === $oAccount->isNickExists($this->pA['nickname'],$isExists)){
                $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
            }
            if($isExists){
                $this->api_output(ML_RCODE_FAIL);
            }

            $accountRes = $oAccount->updateNick($uid, $this->pA['nickname']);
            if(false === $accountRes){
                $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
            }
            $this->loginProxy( modify_login, array('nickname'=>$this->pA['nickname']));
            $rdsHash=new ml_model_rdsHash();
            $rdsHash->updateNick($uid, $this->pA['nickname']);
            ml_tool_sendMq::update_atnick_nick($this->pA['nickname'], $uid);
        }
        $briefRes = $oBrief->updatePerson($uid, $this->pB);
        $extRes = $oExt->updatePerson($uid , $this->pE);

        if(false!=$briefRes && false!=$extRes)
        {
            
            $this->api_output(ML_RCODE_SUCC);
        }
        else 
        {
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
    }
}

new aj_personSetting();
